<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>设置景点价格/库存</title>
    {template 'stourtravel/public/public_min_js'}
    {php echo Common::getCss('style.css,base.css,base2.css,base_new.css,calendar-price-jquery.min.css'); }
    {Common::getScript("jquery.validate.js,choose.js,product_add.js,jquery.upload.js,DatePicker/WdatePicker.js")}
    {Common::css_plugin('spot_edit.css','spot')}
    {php echo Common::js_plugin('jquery.datetimepicker.full.js','spot');}
    {php echo Common::css_plugin('jquery.datetimepicker.css','spot');}
    <style>
        .dialog-body {
            height: 100%;
            position: relative;
        }
        .dialog-body .save-con {
            position: fixed;
            bottom: 0;
            background: #fff;
            width: 100%;
            text-align: right;
            border-top: solid 1px #e5e5e5;
            padding-top: 10px;
            z-index: 999;
        }

        .dialog-body .product-add-div {
            padding-bottom: 50px;
        }

        .rule-item {
            display: flex;
            justify-content: flex-start;
            align-items: center;
        }

        .rule-item .rule-item-hd {
            width: 100px;
            text-align: right;
        }
        .suit-block th,.suit-block td{
            border:1px solid #DBDBDB
        }
    </style>
</head>
<body>
<div class="dialog-body">
    <div class="product-add-div">
        <form id="suit_form">
            <ul class="info-item-block" style="padding-top:0">
                <input type="hidden" name="ticket_id" value="{$sku['ticket_id']}" />
                <input type="hidden" name="sku_id" value="{$sku['sku_id']}" />
                <input type="hidden" name="spot_id" value="{$spot_id}" />
                <input type="hidden" name="day" value="{$date}" />
                {if !empty($price)}
                <li>
                    <div class="item-hd">开关状态：</div>
                    <div class="item-bd">
                        <span class="select-box w100">
                            <select class="select" name="is_open">
                                <option value="0">已下架</option>
                                <option {if $price['is_open'] == 1}selected{/if} value="1">已上架</option>
                            </select>
                        </span>
                    </div>
                </li>
                {/if}
                <li>
                    <div class="item-hd"><i class="c-red">*</i>维护价格库存：</div>
                    <div class="item-bd">
                        <table class="mt-10 suit-edit-block suit-block"  padding_strong=t0Rrqt >
                            <thead id="suit_thead">
                            <tr class="suit-edit-th">
                                <th class="text-c">
                                    销售属性
                                </th>
                                {if $sku['settlement_type'] == 2}
                                <th class="user-click text-c suit-setting">
                                    <i class="c-red">*</i>售价
                                </th>
                                <th class="user-click text-c suit-setting">
                                    <i class="c-red">*</i>抽佣方式
                                </th>
                                <th class="user-click text-c suit-setting">
                                    <i class="c-red">*</i>比例/金额
                                </th>
                                {else}
                                <th class="user-click text-c suit-setting">
                                    <i class="c-red">*</i>底价
                                </th>
                                <th class="user-click text-c suit-setting">
                                    <i class="c-red">*</i>利润
                                </th>
                                <th class="user-click text-c">
                                    卖价
                                </th>
                                {/if}
                                <th class="user-click text-c suit-setting">
                                    库存模式
                                </th>
                                <th class="text-c">
                                    库存数量
                                </th>
                                <th class="text-c">
                                    预警库存数量
                                </th>
                            </tr>
                            </thead>
                            <tbody id="suit_tbody">
                            <tr id="sku_{$sku['ticket_id']}_{$sku['sku_id']}" class="show-sku">
                                <td class="text-c suit-edit-tb">{$sku['title']}</td>
                                {if $sku['settlement_type'] == 2}
                                <td class="text-c suit-edit-tb">
                                    <label>
                                        <input name="basic_price" type="number" value="{$price['adultbasicprice']}" maxlength="9" placeholder="售价" class="input-text"/>
                                    </label>
                                </td>
                                <td class="text-c suit-edit-tb">
                                    <select name="settlement_key[{$sku['ticket_id']}_{$sku['sku_id']}]" class="select settlement_key" onchange="settings_switch($(this),{$sku['ticket_id']},{$sku['sku_id']})">
                                        <option value="1" {if $settlement_type['settlement_key']!=2}selected{/if}>百分比抽佣</option>
                                        <option value="2" {if $settlement_type['settlement_key']==2}selected{/if}>固定金额抽佣</option>
                                    </select>
                                </td>
                                <td class="text-c suit-edit-tb">
                                    <input type="text" onkeyup="number_constraint($(this),{$sku['ticket_id']},{$sku['sku_id']})" value="{$settlement_type['settlement_val']}" placeholder="比例0-100" name="settlement_val[{$sku['ticket_id']}_{$sku['sku_id']}]" class="input-text settlement_val">
                                </td>
                                {else}
                                <td class="text-c suit-edit-tb">
                                    <label>
                                        <input name="basic_price" type="number" value="{$price['adultbasicprice']}" maxlength="9" placeholder="底价" class="input-text"/>
                                    </label>
                                </td>
                                <td class="text-c suit-edit-tb">
                                    <label>
                                        <input name="profit" type="number" value="{$price['adultprofit']}" maxlength="9" placeholder="利润" class="input-text"/>
                                    </label>
                                </td>
                                <td class="text-c suit-edit-tb sale-price">
                                    {$price['adultprice']}
                                </td>
                                {/if}
                                <td class="text-c suit-edit-tb">
                                    <label class="select-box">
                                        <select class="select" name="stock_mod">
                                            {if $price['stock_mod']}
                                            <option {if $price['stock_mod']}selected{/if} value="3">总库存</option>
                                            {else}
                                            <option value="1">日库存</option>
                                            <option {if $price['number'] == -1}selected{/if} value="2">无限库存</option>
                                            {/if}
                                        </select>
                                    </label>
                                </td>
                                <td class="text-c suit-edit-tb">
                                    <label class="stock-setting-1 {if $price['stock_mod'] || $price['number'] == -1}hide{/if}">
                                        <input name="stock" type="number" value="{if $price['number'] != -1}{$price['number']}{/if}" maxlength="9" placeholder="库存数量" class="input-text"/>
                                    </label>
                                    <span class="stock-setting-2 {if $price['number'] != -1}hide{/if}">无需维护</span>
                                    <label class="stock-setting-3 {if !$price['stock_mod']}hide{/if}">
                                        <input name="total_stock" type="number" value="{if $price['number'] != -1}{$price['number']}{/if}" maxlength="9" placeholder="库存数量" class="input-text"/>
                                    </label>
                                </td>
                                <td class="text-c suit-edit-tb">
                                    <label class="stock-mod-1 {if $price['stock_mod'] || $price['number'] == -1}hide{/if}">
                                        <input name="warning_stock" type="number" value="{if $price['number'] != -1}{$price['warning_stock']}{/if}" maxlength="9" placeholder="预警库存数量" class="input-text"/>
                                    </label>
                                    <span class="stock-mod-2 {if $price['number'] != -1}hide{/if}">无需维护</span>
                                    <span class="stock-mod-3 {if !$price['stock_mod']}hide{/if}">无需维护</span>
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                </li>
            </ul>
        </form>
    </div>

    <div class="save-con">
        {if !empty($price)}
     <!--   <a href="javascript:;" class="btn btn-primary size-M fl change-status-btn" data-status="1">上架</a>
        <a href="javascript:;" class="btn btn-grey size-M mr-10 fl change-status-btn" data-status="0">下架</a>-->
        <a href="javascript:;" class="btn btn-grey size-M mr-10" id="clear_price_btn">清除报价</a>
        {/if}
        <a href="javascript:;" class="btn btn-grey size-M mr-10" id="cancel_btn">取消</a>
        <a href="javascript:;" class="btn btn-primary size-M" id="confirm_btn">保存</a>
    </div>
</div>
</body>
<script type="text/javascript">
    $(document).ready(function() {
        var suitTbody = $("#suit_tbody");

        $('.save-con').on('click', '.change-status-btn', function () {
            // 上下架
            var postData = {
                ticket_id:"{$sku['ticket_id']}",
                sku_id:"{$sku['sku_id']}",
                status: $(this).data('status'),
                day: '{$date}'
            };
            ST.Util.showLoading('保存中');
            $.post(SITEURL + 'spot/admin/spot/ajax_change_price_status', postData, function (res) {
                if (res.status) {
                    ST.Util.showSuccessMsg(res.msg);
                    setTimeout(function () {
                        ST.Util.responseDialog({},true);
                    }, 1500)
                } else {
                    ST.Util.showFailureMsg(res.msg);
                }
            }, 'json')
        }).on('click', '#clear_price_btn', function () {
            // 清除报价
            var postData = {
                ticket_id:"{$sku['ticket_id']}",
                sku_id:"{$sku['sku_id']}",
                day: '{$date}'
            };
            ST.Util.showLoading('保存中');
            $.post(SITEURL + 'spot/admin/spot/ajax_clear_day_price', postData, function (res) {
                if (res.status) {
                    ST.Util.showSuccessMsg(res.msg);
                    setTimeout(function () {
                        ST.Util.responseDialog({},true);
                    }, 1500)
                } else {
                    ST.Util.showFailureMsg(res.msg);
                }
            }, 'json')
        }).on('click', '#cancel_btn', function () {
            // 取消操作按钮
            ST.Util.closeDialog();
        }).on('click', '#confirm_btn', function () {
            // 提交保存按钮

            // 价格库存设置
            var priceRes = checkPriceSet()
            if(!priceRes.status) {
                ST.Util.showFailureMsg(priceRes.msg);
                return false;
            }

            var frm_data = $("#suit_form").serialize();
            ST.Util.showLoading('保存中');
            $.ajax({
                data: frm_data,
                dataType: 'json',
                type: 'post',
                url: SITEURL + 'spot/admin/spot/ajax_save_price',
                success: function (result) {
                    if (result.status) {
                        ST.Util.showSuccessMsg(result.msg);
                        setTimeout(function () {
                            ST.Util.responseDialog({},true);
                        }, 1500)
                    } else {
                        ST.Util.showMsg(result.msg, 5, 1000);
                    }
                }
            })
        })

        // 检查价格库存设置
        function checkPriceSet() {
            try {
                var skus = suitTbody.find('.show-sku'), settlementType = "{$sku['settlement_type']}";
                skus.each(function (index, item) {
                    var stockModObj = $(item).find('select[name*=stock_mod]')[0],
                        stockObj = $(item).find('input[name*=stock]')[0],
                        warningStockObj = $(item).find('input[name*=warning_stock]')[0],
                        stockMod = stockModObj.value,
                        stock = stockObj.value,
                        warningStock = warningStockObj.value,
                        basicPriceObj = $(item).find('input[name*=basic_price]')[0],
                        basicPrice = basicPriceObj.value;

                    if (settlementType === '2') {
                        if (basicPrice < 0 || basicPrice === '') {
                            basicPriceObj.focus();
                            throw '售价必须大于等于 0';
                        }
                    } else {
                        var profitObj = $(item).find('input[name*=profit]')[0],
                            profit = profitObj.value;
                        if (basicPrice < 0 || basicPrice === '') {
                            basicPriceObj.focus();
                            throw '底价必须大于等于 0';
                        }
                        if (profit < 0 || profit === '') {
                            profitObj.focus();
                            throw '利润必须大于等于 0';
                        }
                    }
                    if (stockMod === '1') {
                        if (stock < 0 || stock === '') {
                            stockObj.focus();
                            throw '库存必须大于等于 0';
                        }

                        if (warningStock) {
                            if (warningStock < 0) {
                                warningStockObj.focus();
                                throw '预警库存必须大于 0';
                            }
                            stock = parseInt(stock)
                            warningStock = parseInt(warningStock)
                            if(stock > 0 && warningStock >= stock) {
                                warningStockObj.focus();
                                throw '预警库存必须小于库存';
                            }
                        }
                    }
                })
                return {status: true, msg: ''}
            } catch (e) {
                return {status: false, msg: e}
            }
        }

        suitTbody.on('blur', "input[name*=basic_price]", function () {
            formatMoney(this);

            updateSalePrice();
        }).on('blur', "input[name*=profit]", function () {
            formatMoney(this);

            updateSalePrice();
        }).on('change', "select[name*=stock_mod]", function () {
            changeStockMod(this)
        })

        // 切换库存mod
        function changeStockMod(obj) {
            var val = $(obj).val()
                ,stockId = ".stock-setting-"+val
                ,stockModId = ".stock-mod-"+val;
            $(stockId).removeClass('hide');
            $(stockId).siblings().addClass('hide');
            $(stockModId).removeClass('hide');
            $(stockModId).siblings().addClass('hide');
        }

        // 重新更新卖价
        function updateSalePrice() {
            var suits = suitTbody.find('tr');

            suits.each(function (index, suit) {
                var basicPrice = $(suit).find('input[name*=basic_price]').val()
                    , profit =  $(suit).find('input[name*=profit]').val()
                    , salePriceObj =  $(suit).find('.sale-price'),
                    salePrice = '--'

                if(basicPrice && profit) {
                    salePrice = parseFloat(basicPrice) + parseFloat(profit);
                    salePrice = salePrice.toFixed(2)
                }

                salePriceObj.text(salePrice)
            })
        }

        // 对输入金额进行校验处理
        function formatMoney(obj) {
            obj.value = obj.value.replace(/[^\d.]/g, "");//清除"数字"和"."以外的字符
            obj.value = obj.value.replace(/^\./g, "");//验证第一个字符是数字而不是字符
            obj.value = obj.value.replace(/\.{2,}/g, ".");//只保留第一个.清除多余的
            obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
            obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');//只能输入两个小数
        }
    })

    function number_constraint(obj,ticket_id,sku_id)
    {
        var settings_type = $("[name='settlement_key["+ticket_id+"_"+sku_id+"]']").find('option:selected').val();
        var val = obj.val();
        if(settings_type==='1'){
            //this.value=this.value.replace(/\D/g,'')
            val = val.replace(/\D/g,'')
            if(val>100){
                val = 100
            }else if(val<0){
                val = 0
            }
        }else{
            //value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')
            val = val.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')
        }
        obj.val(val);
    }
</script>
</html>